/**
 * Author: Shen Yanqiu
 * Date: 2018-02-24
 * Time: 17:09
 *
 */
import modelExtend from 'dva-model-extend';
import {pageModel} from '../common';
import {message} from 'antd';
import { delay, routerTool } from "utils";

export default modelExtend(pageModel, {
  namespace: 'personnelStatistics',
  state: {
    statistics: {
      startPersonCount: 0,
      present: 0,
      lastWeekPersonCount: 0,
      lastMonthPersonCount: 0,
      lastWeekDimissionCount: 0,
      lastMonthDimissionCount: 0,
      lastWeekDimissionRate: 0,
      lastMonthDimissionRate: 0,
    },
  },
  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === routerTool.getPathByName('PersonnelStatistics')) {
          dispatch({ type: 'firstLoad' });
        }
      });
    },
  },
  effects: {
    * firstLoad ({ payload = {} }, { put, call }) {
      yield put({ type: 'loadStatisticsInfo' });
      yield put({ type: 'loadTableList', payload: { current: 0, pageSize: 10 } });
      yield delay(1000);
    },
    * loadStatisticsInfo ({ payload = {} }, { put, call }) {
      const statistics = {
        startPersonCount: 74209,
        present: 23590,
        lastWeekPersonCount: 157,
        lastMonthPersonCount: 33,
        lastWeekDimissionCount: 65,
        lastMonthDimissionCount: 37,
        lastWeekDimissionRate: 65,
        lastMonthDimissionRate: 78,
      };
      yield put({ type: 'updateStatistics', payload: { statistics } });
    },
    * loadTableList ({ payload = {} }, { put, call }) {
      const data = {
        total: 22,
        list: [{
          key: '1',
          xuhao: '1',
          bumen: '新闻部',
          presentPerson: 120,
          dimission: 12,
        }, {
          key: '2',
          xuhao: '2',
          bumen: '活动部',
          presentPerson: 125,
          dimission: 3,
        }, {
          key: '3',
          xuhao: '3',
          bumen: '活动部',
          presentPerson: 159,
          dimission: 8,
        }, {
          key: '4',
          xuhao: '4',
          bumen: '活动部',
          presentPerson: 122,
          dimission: 21,
        }, {
          key: '5',
          xuhao: '5',
          bumen: '活动部',
          presentPerson: 100,
          dimission: 9,
        }, {
          key: '6',
          xuhao: '6',
          bumen: '活动部',
          presentPerson: 110,
          dimission: 10,
        }, {
          key: '7',
          xuhao: '7',
          bumen: '活动部',
          presentPerson: 90,
          dimission: 13,
        }],
      }
      yield put({
        type: 'loadSuccess',
        payload: {
          list: data.list,
          pagination: {
            current: payload.current,
            pageSize: payload.pageSize,
            total: data.total,
          },
        },
      });
      yield delay(2000);
    },
  },
  reducers: {
    updateStatistics (state, { payload: { statistics } }) {
      return { ...state, ...{ statistics } };
    },
  },
});
